package com.mdoffice.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mdoffice.beans.PatientBean;
import com.mdoffice.dao.impl.PatientDAOImpl;
import com.mdoffice.dao.interfaces.PatientDAO;
import com.mdoffice.vo.PatientListVo;

public class PatientInsert extends HttpServlet {
	public PatientInsert() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PatientBean pb = new PatientBean();
		try {
			int patientID = Integer.parseInt(request.getParameter("patientID"));
			System.out.println("patient ID--" + patientID);
			pb.setPatientID(patientID);
			PatientDAO pdao = new PatientDAOImpl();
			PatientListVo pvo = pdao.select(pb.getPatientID());
			HttpSession session = request.getSession();
			System.out.println("drugs---in servlet" + pvo.getDrugs());
			session.setAttribute("pvo", pvo);
			response.sendRedirect("./PatientList.jsp");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			response.sendRedirect("./fail.jsp");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PatientBean pb = new PatientBean();
		pb.setPname(request.getParameter("pname").trim());
		pb.setPaddr(request.getParameter("paddr").trim());
		pb.setDiesease(request.getParameter("diesease").trim());
		String drugs = "";
		String drug[] = request.getParameterValues("drugs");
		for (int i = 0; i < drug.length; i++) {
			drugs += drug[i] + ",";
		}
		try {
			pb.setDrugs(drugs);
			PatientDAO pdao = new PatientDAOImpl();
			int patientID = pdao.insert(pb);
			pb.setPatientID(patientID);
			System.out.println("patientID=====" + pb.getPatientID());
			HttpSession session = request.getSession();
			session.setAttribute("pbean", pb);
			response.sendRedirect("./patientinsert.jsp");
		} catch (Exception e) {
			// TODO: handle exception
			response.sendRedirect("./fail.jsp");
		}
	}

	public void init() throws ServletException {
		// Put your code here
	}
}